﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DAL;
using System.Data;

namespace BLL
{
    public class ScheduleBUS
    {
        public static List<Schedule> SearchSchedule(int StationFrom, int StationTo, String date, int CoachTypeID)
        {
            ScheduleDAL scheduleDal = new ScheduleDAL();
            ScheduleDetailDAL scheduleDetailDal = new ScheduleDetailDAL();
            RouteDetailDAL routeDetailDal = new RouteDetailDAL();
            StationDAL stationDal = new StationDAL();
            List<ScheduleDetail> ListScheduleDetail = scheduleDetailDal.GetList_ScheduleDetail("DATEDIFF(dd, FromTime,convert(datetime,'" + date.ToString() + "',103)) = 0", "ScheduleID");//.Search_Schedule_By_CoachType(CoachTypeID);
            List<Schedule> ListSchedule = (List<Schedule>)((from r in ListScheduleDetail select new Schedule(r.ScheduleID)).Distinct().ToList());

            List<Schedule> rs = new List<Schedule>();
            DateTime d = DateTime.ParseExact(date, "dd/MM/yyyy", null);
            foreach (Schedule item in ListSchedule)
            {
                TrainDiagram trainDiagram = scheduleDal.GetTrainDiagram(item.ScheduleID);

                if (validate_date(trainDiagram, StationFrom, StationTo, d))
                {
                    foreach (Coaches_2 c in trainDiagram.CoachesList)
                    {
                        bool f = false;
                        if (c.CoachTypeID == CoachTypeID)
                        {
                            foreach (Seates_2 s in c.Seates)
                            {
                                if (SearchSeates(s.lst, StationFrom, StationTo))
                                {
                                    List<Station> station = RouteBUS.Search_Start_End_Station(trainDiagram.TrainID);
                                    item.FromStationName = station[0].StastionName;
                                    item.ToStationName = station[1].StastionName;
                                    item.TrainID = trainDiagram.TrainID;
                                    item.StartDate = trainDiagram.StartDate;
                                    item.EndDate = trainDiagram.EndDate;
                                    rs.Add(item);
                                    f = true;
                                    break;
                                }
                            }
                        }
                        if (f) break;
                    }
                }
            }
            return rs;
        }

        public static bool validate_date(TrainDiagram trainDiagram, int StationA, int StationB, DateTime Date)
        {
            try
            {
                List<SeatesStatusDetail> lst = trainDiagram.CoachesList[0].Seates[0].lst;
                for (int i = 0; i < lst.Count; i++)
                {
                    if (lst[i].FromStation == StationA && (lst[i].FromTime.Date - Date.Date).Days == 0)
                    {
                        foreach (ScheduleDetail item in lst)
                        {
                            if (item.ToStation == StationB)
                            {
                                return true;
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            { }
            return false;
        }

        public static bool SearchSeates(List<SeatesStatusDetail> lst, int StationA, int StationB)
        {
            bool flag = false;
            for (int i = 0; i < lst.Count; i++)
            {
                if (lst[i].FromStation == StationA)
                {
                    flag = true;
                    for (int j = 0; j < lst.Count; j++)
                    {
                        if (lst[i].FromTime <= lst[j].ToTime)
                        {
                            if (lst[j].Status == 0)
                            {
                                return false;
                            }
                            if (lst[j].ToStation == StationB)
                            {
                                break;
                            }
                        }
                    }
                    if (flag) return true;
                    break;
                }
            }
            return false;
        }

        public static TrainDiagram GetTrainDiagram(int ScheduleID)
        {
            ScheduleDAL scheduleDal = new ScheduleDAL();
            TrainDiagram trainDiagram = scheduleDal.GetTrainDiagram(ScheduleID);
            return trainDiagram;
        }

        public static DataTable GetScheduleDetail(int ScheduleID, int CoachTypeID)
        {
            ScheduleDetailDAL scheduleDetailDal = new ScheduleDetailDAL();
            return scheduleDetailDal.GetTableScheduleDetail_View_ByID(ScheduleID, CoachTypeID);
        }

        //duc
        public static int Insert_Schedule_Select(Schedule temp)
        {
            try
            {
                ScheduleDAL scheduleDal = new ScheduleDAL();
                return scheduleDal.Insert_Schedule_Select(temp);
            }
            catch
            {
                return -1;
            }
        }

        public static bool Insert_ScheduleDetail(ScheduleDetail temp)
        {
            try
            {
                ScheduleDetailDAL scheduleDetailDal = new ScheduleDetailDAL();
                scheduleDetailDal.Insert_ScheduleDetail(temp);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Insert_CoachOfSchedule(CoachOfSchedule temp)
        {
            try
            {
                CoachOfScheduleDAL coachOfScheduleDAL = new CoachOfScheduleDAL();
                coachOfScheduleDAL.Insert_CoachOfSchedule(temp);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static DataTable GetScheduleDetail(int ScheduleID)
        {
            ScheduleDetailDAL scheduleDetailDal = new ScheduleDetailDAL();
            return scheduleDetailDal.GetTableScheduleDetail_View_ByID(ScheduleID);
        }

        public static Schedule Get_Schedule_By_ID(int ScheduleID)
        {
            ScheduleDAL sdal = new ScheduleDAL();
            return sdal.Get_Schedule_By_ID(ScheduleID);
        }

        public static bool Update_Schedule(Schedule s)
        {
            try
            {
                ScheduleDAL sdal = new ScheduleDAL();
                sdal.Update_Schedule(s);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
